package kr.saeildamil.androidstudy.ch12;

import java.util.ArrayList;

import kr.saeildamil.androidstudy.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class MultiAdapterEx extends BaseAdapter {
	LayoutInflater mInflater;
	ArrayList<ListItem> arScr;

	public MultiAdapterEx(Context context, ArrayList<ListItem> atItem) {
		super();
		this.mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.arScr = atItem;
	}

	public int getCount() {
		return arScr.size();
	}

	public Object getItem(int position) {
		return arScr.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public int getItemViewType(int position) {
		return arScr.get(position).Type;
	}

	public int getViewTypeCount() {
		return 2;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			int res = 0;
			switch (arScr.get(position).Type) {
			case 0:
				res = R.layout.ch12_textedit;
				break;
			case 1:
				res = R.layout.ch12_btnicon;
				break;
			}
			convertView = mInflater.inflate(res, parent, false);
		}

		switch (arScr.get(position).Type) {
		case 0:
			TextView tv = (TextView) convertView
					.findViewById(R.id.ch12_textedit_text);
			tv.setText(arScr.get(position).Text);

			EditText et = (EditText) convertView
					.findViewById(R.id.ch12_textedit_edit);
			et.setText(arScr.get(position).EditText);
			break;
		case 1:
			Button btn = (Button) convertView
					.findViewById(R.id.ch12_btnicon_btn);
			btn.setText(arScr.get(position).BtnText);

			ImageView img = (ImageView) convertView
					.findViewById(R.id.ch12_btnicon_img);
			img.setImageResource(arScr.get(position).IconRes);
			break;
		}

		return convertView;
	}

}
